package com.gm.lib.data;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.gm.common.context.GlobalContext;


/**
 * 用户存储类
 *
 * @author haifei.zhang
 * @date 2013-9-8
 */
public class UserPreference {

    Context mContext;
    SharedPreferences mSharedPreferences;

    private static final String fileName = "user_preference";
    private String KEY_USER_NAME = "KEY_USER_NAME";
    private String KEY_NICK_NAME = "KEY_NICK_NAME";
    private String KEY_UID = "KEY_UID";
    private String KEY_AVATAR = "KEY_AVATAR";
    private String KEY_TOKEN = "KEY_TOKEN"; //加密的 key code，用于向服务器请求数据时使用


    private String DefaultUid = "0"; // 用户未登录时
    private String DefaultToken = ""; //

    private UserPreference(Context context) {
        mContext = context;
        mSharedPreferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);// PreferenceManager.getDefaultSharedPreferences(mContext);
    }

    private static UserPreference mInstance = null;

    public static UserPreference getInstance() {
        if (mInstance == null) {
            synchronized (UserPreference.class) {
                if (mInstance == null) {
                    mInstance = new UserPreference(GlobalContext.getContext());
                }
            }
        }
        return mInstance;
    }

    /**
     * 判断是否登录
     *
     * @return
     */
    public boolean isLogin() {
        if (getUserUid().length() > 1) {
            return true;
        } else {
            return false;
        }
    }

    public void addUserName(String username) {
        Editor e = mSharedPreferences.edit();
        e.putString(KEY_USER_NAME, username);
        e.commit();
    }

    public void removeUserName() {
        Editor e = mSharedPreferences.edit();
        e.remove(KEY_USER_NAME);
        e.commit();
    }

    public String getUserName() {
        return mSharedPreferences.getString(KEY_USER_NAME, "");
    }

    public void addNickName(String username) {
        Editor e = mSharedPreferences.edit();
        e.putString(KEY_NICK_NAME, username);
        e.commit();
    }

    public void removeNickName() {
        Editor e = mSharedPreferences.edit();
        e.remove(KEY_NICK_NAME);
        e.commit();
    }

    public String getNickName() {
        return mSharedPreferences.getString(KEY_NICK_NAME, "");
    }

    public void addUserUid(String userUid) {
        Editor e = mSharedPreferences.edit();
        e.putString(KEY_UID, userUid);
        e.commit();
    }

    public void removeUserUid() {
        Editor e = mSharedPreferences.edit();
        e.remove(KEY_UID);
        e.commit();
    }

    public String getUserAvatar() {
        return mSharedPreferences.getString(KEY_AVATAR, "");
    }

    public void addUserAvatar(String Avatar) {
        Editor e = mSharedPreferences.edit();
        e.putString(KEY_AVATAR, Avatar);
        e.commit();
    }

    public void removeUserAvatar() {
        Editor e = mSharedPreferences.edit();
        e.remove(KEY_AVATAR);
        e.commit();
    }

    public String getUserUid() {
        return mSharedPreferences.getString(KEY_UID, DefaultUid);
    }

    public void addToken(String code) {
        Editor e = mSharedPreferences.edit();
        e.putString(KEY_TOKEN, code);
        e.commit();
    }

    public void removeToken() {
        Editor e = mSharedPreferences.edit();
        e.remove(KEY_TOKEN);
        e.commit();
    }

    public String getToken() {
        return mSharedPreferences.getString(KEY_TOKEN, DefaultToken);
    }

    public void clearSharedPrefs() {
        Editor e = mSharedPreferences.edit();
        e.clear();
        e.commit();
    }

    /**
     * 将网络上的html5面页请求时，加上token
     *
     * @param url
     * @return
     */
    public String getUrlWidthToken(String url) {
        StringBuilder returnUrl = new StringBuilder(url);
        if (url.contains("?")) {
            returnUrl.append("&token=" + getToken()).append("&uid=" + getUserUid());
        } else {
            returnUrl.append("?token=" + getToken()).append("&uid=" + getUserUid());
        }
        return returnUrl.toString();
    }




}
